<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Account
 *
 * @author MAI ANH
 */

namespace User\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use User\Form\Validate\RegisterValidate;

class AccountTable extends AbstractTableGateway {

    protected $table = 'account';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new RegisterValidate());
        $this->initialize();
    }

    public function getAccount($id) {
        $id = (int) $id;
        $rowset = $this->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    public function insertAccount($params) {
        $data = array(
            'user_name' => $params['user_name'],
            'password' => $params['password'],
            'mail' => $params['mail'],
            'first_name' => $params['first_name'],
            'last_name' => $params['last_name'],
            'birth_day' => $params['birth_day'],
        );
        $id = (int) $params['id'];
        if ($id == 0) {
            $this->insert($data);
        } else {
            if ($this->getAccount($id)) {
                $this->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

}

?>
